package com.ly.rabbitmq.spring;

/**
 * @Classname Comuser
 * @Description TODO
 * @Date 2020/11/20 20:53
 * @Author 冷心影翼
 */

import com.rabbitmq.client.Channel;


import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

//@Component
public class Consumer {

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@RabbitListener(queues = {"ttlQueue"},containerFactory = "bootContainerFactory")
	public void ListenQueue2(MessageData messageData,Channel channel, Message message) throws IOException, InterruptedException {
		TimeUnit.SECONDS.sleep(2000);
		channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);

		System.out.println(messageData);
	}


	@RabbitListener(queues = {"bootQueue"},containerFactory = "bootContainerFactory")
	public void ListenQueue(MessageData messageData,Channel channel, Message message) throws IOException, InterruptedException {
		TimeUnit.SECONDS.sleep(3);
		try{
			System.out.println(messageData);
			int i = 1/0;
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
		}catch(Exception e){
			System.out.println(e);
			channel.basicNack(message.getMessageProperties().getDeliveryTag(),true,false);
		}finally {

		}
	}
}
